package com.erp.erp_ui.CustomerComplaint;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.CustomerComplaint.*;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.CustomerComplaint.ICustomerComplaintFileService;
import com.erp.erp_servers.CustomerComplaint.ICustomerComplaintProductService;
import com.erp.erp_servers.CustomerComplaint.ICustomerComplaintService;
import com.erp.erp_servers.Ftp.IFtpService;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.business.req.OfferProductStatisticsReq;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "complaint")
@Api( tags = "客诉记录")
public class CustomerComplaintControl extends Base {
    @Autowired
    private ICustomerComplaintService customerComplaintService;
    @Autowired
    private ICustomerComplaintProductService customerComplaintProductService;
    @Autowired
    private ICustomerComplaintFileService customerComplaintFileService;
    @Autowired
    private IFtpService ftpService;


    private SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    /**
     * 新增客诉记录
     * @param customerComplaint
     * @return
     */
    @RequestMapping(value = "insert" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增客诉记录", notes = "新增客诉记录", httpMethod = "POST")
    public R<Integer> insert(@RequestBody CustomerComplaintEntity customerComplaint){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                if(StringUtils.isEmpty(customerComplaint.getOddNumber())){
                    return  R.buildError(ResultStatusEnum.NOT_EMPTY);
                }
                boolean isExist = customerComplaintService.isExist(customerComplaint.getOddNumber());
                if(isExist){
                    return  R.buildError(ResultStatusEnum.BILL_NO_REPEAT);
                }
                UserEntity user = (UserEntity )cacheUser;
                customerComplaint.setExecutor(user.getCode());
                if (customerComplaint.getExecutor()==null || customerComplaint.getExecutor().equals("")){
                    customerComplaint.setExecutor(user.getCode());
                }
                if(customerComplaint.getCreateTime()==null){
                    customerComplaint.setCreateTime(new Date());
                }
                customerComplaint.setBoxStatus("1");//草稿
                int num = customerComplaintService.insertProAndFile(customerComplaint);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询客诉记录
     * @param id
     * @return
     */
    @RequestMapping(value = "selectByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询客诉记录",notes = "按id查询客诉记录",httpMethod = "POST")
    public R<CustomerComplaintEntity> selectByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            CustomerComplaintEntity customerComplaint = customerComplaintService.selectByPrimaryKey(id);
            if (customerComplaint==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(customerComplaint);
        } catch (NumberFormatException e1){
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
        } catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新客诉记录
     * 部分字段
     * @param customerComplaint
     * @return
     */
    @RequestMapping(value = "update")
    @ResponseBody
    @ApiOperation(value = "按id更新客诉记录", notes = "按id更新客诉记录", httpMethod = "POST")
    public R<Integer> update(@RequestBody CustomerComplaintEntity customerComplaint){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                customerComplaint.setExecutor(user.getCode());
                int num = customerComplaintService.updateInfoById(customerComplaint);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新客诉审核状态
     * 部分字段
     * @param ccVo
     * @return 操作记录数
     */
    @RequestMapping(value = "updateBoxStatusBykey")
    @ResponseBody
    @ApiOperation(value = "按id更新客诉审核状态", notes = "按id更新客诉审核状态", httpMethod = "POST")
    public R<Integer> updateBoxStatusBykey(@RequestBody CustomerComplaintVo ccVo){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                if (null==ccVo || null == ccVo.getId() || null == ccVo.getBoxStatus()){
                    return  R.buildError(ResultStatusEnum.PARAMETER_ERROR);
                }
                int num = customerComplaintService.updateBoxStatusBykey(ccVo);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除询盘登记-备注
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除客诉记录", notes = "按id删除客诉记录", httpMethod = "POST")
    public R<Integer> delete(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                CustomerComplaintEntity customerComplaint = customerComplaintService.selectByPrimaryKey(id);
                int num = 0;
                if(customerComplaint!=null){
                    num = customerComplaintService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按条件查询客诉记录
     * @param
     * @return
     */
    @RequestMapping(value = "selectList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按条件查询客诉记录", notes ="按条件查询客诉记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="boxStatus",value = "单据类型"),
            @ApiImplicitParam(name="numberLike",value = "单据信息"),
            @ApiImplicitParam(name="executor",value = "所属操作员"),
            @ApiImplicitParam(name="salesman",value = "所属业务员"),
            @ApiImplicitParam(name="startCreateTime",value = "创建开始时间"),
            @ApiImplicitParam(name="endCreateTime",value = "创建结束时间"),
            @ApiImplicitParam(name="pageNum",value = "当前页"),
            @ApiImplicitParam(name="pageSize",value = "页内条数")
    })
    public R selectList(@RequestBody CustomerComplaintVo vo){
        try {
            if (vo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            if (vo.getStartCreateTime()!=null){
                Date startDate = DateUtils.getStartOfDay(vo.getStartCreateTime());
                vo.setStartCreateTime(startDate);
                if (vo.getEndCreateTime()==null){
                    vo.setEndCreateTime(new Date());
                }
                Date endDate = DateUtils.getEndOfDay(vo.getEndCreateTime());
                vo.setEndCreateTime(endDate);
            }
            PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
            PageInfo<CustomerComplaintEntity> pageInfo = customerComplaintService.selectCustomerComplainPage(vo);
            if(pageInfo.getTotal()==0){
                if(pageInfo.getList()==null || pageInfo.getTotal()==0){
                    return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
                }
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增客诉产品记录
     * @param customerComplaintProduct
     * @return
     */
    @RequestMapping(value = "insertProduct" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增客诉记录", notes = "新增客诉记录", httpMethod = "POST")
    public R<Integer> insertProduct(@RequestBody CustomerComplaintProductEntity customerComplaintProduct){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = customerComplaintProductService.insertSelective(customerComplaintProduct);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (DataIntegrityViolationException e1){
            //完整性违规 添加的数据 违反外键约束
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询客诉产品记录
     * @param id
     * @return
     */
    @RequestMapping(value = "selectProductByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询客诉记录",notes = "按id查询客诉产品记录",httpMethod = "POST")
    public R<CustomerComplaintProductEntity> selectProductByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            CustomerComplaintProductEntity customerComplaintProduct = customerComplaintProductService.selectByPrimaryKey(id);
            if (customerComplaintProduct==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(customerComplaintProduct);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新客诉产品记录
     * 部分字段
     * @param customerComplaintProduct
     * @return
     */
    @RequestMapping(value = "updateProduct")
    @ResponseBody
    @ApiOperation(value = "按id更新客诉记录", notes = "按id更新客诉产品记录", httpMethod = "POST")
    public R<Integer> updateProduct(@RequestBody CustomerComplaintProductEntity customerComplaintProduct){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = customerComplaintProductService.updateByPrimaryKeySelective(customerComplaintProduct);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除客诉-产品
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteProduct/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除客诉产品", notes = "按id删除客诉产品", httpMethod = "POST")
    public R<Integer> deleteProduct(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                CustomerComplaintProductEntity customerComplaintProduct = customerComplaintProductService.selectByPrimaryKey(id);
                int num = 0;
                if(customerComplaintProduct!=null){
                    num = customerComplaintProductService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增客诉附件
     * @param customerComplaintFile
     * @return
     */
    @RequestMapping(value = "insertFile" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增客诉附件", notes = "新增客诉附件", httpMethod = "POST")
    public R<Integer> insertFile(@RequestBody CustomerComplaintFileEntity customerComplaintFile){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = customerComplaintFileService.insertSelective(customerComplaintFile);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (DataIntegrityViolationException e1){
            //完整性违规 添加的数据 违反外键约束
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询客诉产品记录
     * @param id
     * @return
     */
    @RequestMapping(value = "selectFileByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询客诉附件",notes = "按id查询客诉附件",httpMethod = "POST")
    public R<CustomerComplaintFileEntity> selectFileByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            CustomerComplaintFileEntity customerComplaintFile = customerComplaintFileService.selectByPrimaryKey(id);
            if (customerComplaintFile==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(customerComplaintFile);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新客诉附件
     * 部分字段
     * @param customerComplaintFile
     * @return
     */
    @RequestMapping(value = "updateFile")
    @ResponseBody
    @ApiOperation(value = "按id更新客诉附件", notes = "按id更新客诉附件", httpMethod = "POST")
    public R<Integer> updateFile(@RequestBody CustomerComplaintFileEntity customerComplaintFile){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = customerComplaintFileService.updateByPrimaryKeySelective(customerComplaintFile);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除客诉-附件
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteFile/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除客诉附件", notes = "按id删除客诉附件", httpMethod = "POST")
    public R<Integer> deleteFile(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                CustomerComplaintFileEntity customerComplaintFile = customerComplaintFileService.selectByPrimaryKey(id);
                int num = 0;
                if(customerComplaintFile!=null){
                    num = customerComplaintFileService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按cid查询客诉-产品列表
     * @param
     * @return
     */
    @RequestMapping(value = "selectProductByCid/{cid}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据cid查询客诉产品记录", notes ="根据cid查询客诉产品记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cid",value = "客诉记录id"),
    })
    public R<List<CustomerComplaintProductEntity>> selectProductByCid(@PathVariable("cid") Integer cid){
        try {
            if (cid==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            List<CustomerComplaintProductEntity> list = customerComplaintProductService.selectProductByCid(cid);
            if (list==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按cid查询客诉附件-列表
     * @param
     * @return
     */
    @RequestMapping(value = "selectFileByCid/{cid}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据cid查询客诉附件", notes ="根据cid查询客诉附件", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cid",value = "客诉记录id"),
    })
    public R<List<CustomerComplaintFileEntity>> selectFileByCid(@PathVariable("cid") Integer cid){
        try {
            if (cid==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            List<CustomerComplaintFileEntity> list = customerComplaintFileService.selectFileByCid(cid);
            if (list==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 分页查询 外销合同列表
     * @param contractInfoVo 查询参数
     * @return
     */
//    @RequestMapping(value = "getContractByPage",method = RequestMethod.GET)
    @GetMapping("/getContractByPage")
    @ResponseBody
    @ApiOperation(value = "按条件查询外销合同", notes ="按条件查询外销合同", httpMethod = "GET")
    public R getContractByPage(ContractInfoVo contractInfoVo){
        try {
            if (contractInfoVo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<ContractInfoVo> pageInfo = customerComplaintProductService.getContractList(contractInfoVo);
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按contractIds查询外销合同对应-产品列表
     * @param
     * @return
     */
//    @RequestMapping(value = "getContractProductByPage",method = RequestMethod.GET)
    @GetMapping("/getContractProductByPage")
    @ResponseBody
    @ApiOperation(value = "按contractIds查询外销合同列表", notes ="按contractIds查询外销合同列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="contractId",value = "外销合同id"),
    })
    public R getContractProductByPage(Integer contractId){
        try {
            if(null == contractId){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<CustomerComplaintProductEntity> pageInfo = customerComplaintProductService.getContractProductInfo(contractId);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按contractIds查询外销合同对应-产品列表
     * @param
     * @return
     */
    @RequestMapping(value = "getProductInfoByContractIds",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按contractIds查询外销合同对应-产品列表", notes ="按contractIds查询外销合同对应-产品列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids",value = "外销合同ids"),
    })
    public R getProductInfoByContractIds(@RequestBody Integer[] ids){
        try {
            if(null == ids){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<CustomerComplaintProductEntity> pageInfo = customerComplaintProductService.getProductInfoByContractIds(Arrays.asList(ids));
            return R.build(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 查询最新单号  累计
     * @return 单号
     */
    @RequestMapping(value = "getLatestOddNumber")
    @ResponseBody
    @ApiOperation(value = "查询最新单号",notes = "查询最新单号",httpMethod = "POST")
    public R<String> selectByKey(){
        try {
            Integer latestOddNumberCount = customerComplaintService.getCountNum();
            int digit = 3;//位数
            int max = Integer.parseInt("1"+String.format("%1$0"+digit+"d",0));//右补0
            while (latestOddNumberCount>=max){
                digit++;
                max = Integer.parseInt("1"+String.format("%1$0"+digit+"d",0));
            }
            String latestOddNumber = "";
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String nowDateStr = sdf.format(d);
            String suffix = String.format("%0"+digit+"d", (latestOddNumberCount+1));//左补齐0
            latestOddNumber = "CAPA"+nowDateStr+suffix;
            return R.build(latestOddNumber);
        } catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 统计  按条件查询客诉
     * @param 条件
     * @return 客诉及对应产品
     */
    @RequestMapping(value = "selectMainAndProductList",method = RequestMethod.POST)
    @ResponseBody
    public R selectMainAndProductList(@RequestBody CustomerComplaintVo vo){
        try {
            if (vo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            if (vo.getStartCreateTime()!=null){
                Date startDate = DateUtils.getStartOfDay(vo.getStartCreateTime());
                vo.setStartCreateTime(startDate);
                if (vo.getEndCreateTime()==null){
                    vo.setEndCreateTime(new Date());
                }
                Date endDate = DateUtils.getEndOfDay(vo.getEndCreateTime());
                vo.setEndCreateTime(endDate);
            }else{
                Map<String, Date> map = DateUtils.getNowMonthNew();
                Date startDate = map.get("startDate");
                Date endDate = map.get("endDate");
                vo.setStartCreateTime(startDate);
                vo.setEndCreateTime(endDate);
            }
            PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
            List<CustomerComplaintAndProduct> customerComplaintList = customerComplaintService.selectCustomerComplainAndProductList(vo);
            //获取分页信息
            PageInfo pageInfo = new PageInfo(customerComplaintList);
            if(CollectionUtils.isEmpty(customerComplaintList)){
                pageInfo.setList(null);
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }else{
                pageInfo.setList(customerComplaintList);
            }
            if(pageInfo.getTotal()==0){
                if(pageInfo.getList()==null || pageInfo.getTotal()==0){
                    return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
                }
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 统计-导出Excel
     * @return 统计信息
     */
//    @RequestMapping(value = "export/selectMainAndProductList",method = RequestMethod.GET)
    @GetMapping(value = "export/selectMainAndProductList")
    @ResponseBody
    public R exportMainAndProductList(CustomerComplaintVo vo){
        try {
            JSONObject obj = new JSONObject();
            JSONArray data = new JSONArray();
            JSONArray titleList = getTitleStatistics();//标题行
            data.add(titleList);
            List<CustomerComplaintAndProduct> customerComplaintList = customerComplaintService.selectCustomerComplainAndProductList(vo);
            if(!CollectionUtils.isEmpty(customerComplaintList)){
                BigDecimal clientDeductionPriceTotal = BigDecimal.ZERO;//客户扣款金额-总和
                BigDecimal supplierDeductionPriceTotal = BigDecimal.ZERO;//供应商扣款金额-总和
                BigDecimal priceTotal = BigDecimal.ZERO;//单价-总和
                for (CustomerComplaintAndProduct ccp : customerComplaintList){
                    JSONArray infoList = new JSONArray();
                    infoList.add(StringUtils.isNotEmpty(ccp.getOddNumber())?ccp.getOddNumber():"");
                    infoList.add(DateUtils.format(ccp.getCreateTime()));
                    infoList.add(StringUtils.isNotEmpty(ccp.getResponsibleParty())?ccp.getResponsibleParty():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getComplaintUnit())?ccp.getComplaintUnit():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getCustomerName())?ccp.getCustomerName():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getExecutor())?ccp.getExecutor():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getSalesman())?ccp.getSalesman():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getContractNo())?ccp.getContractNo():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getProductNo())?ccp.getProductNo():"");
                    BigDecimal cdp = ccp.getClientDeductionPrice() != null ? ccp.getClientDeductionPrice() : BigDecimal.ZERO;
                    clientDeductionPriceTotal = clientDeductionPriceTotal.add(cdp);
                    infoList.add(cdp);
                    BigDecimal sdp = ccp.getSupplierDeductionPrice()!=null?ccp.getSupplierDeductionPrice():BigDecimal.ZERO;
                    supplierDeductionPriceTotal = supplierDeductionPriceTotal.add(sdp);
                    infoList.add(sdp);
                    infoList.add(StringUtils.isNotEmpty(ccp.getSpecNo())?ccp.getSpecNo():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getNormExplain())?ccp.getNormExplain():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getChineseName())?ccp.getChineseName():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getEnglishName())?ccp.getEnglishName():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getChineseDesc())?ccp.getChineseDesc():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getEnglishDesc())?ccp.getEnglishDesc():"");
                    infoList.add(StringUtils.isNotEmpty(ccp.getColour())?ccp.getColour():"");
                    BigDecimal price = ccp.getPrice()!=null?ccp.getPrice():BigDecimal.ZERO;
                    priceTotal = priceTotal.add(price);
                    infoList.add(price);
                    infoList.add(StringUtils.isNotEmpty(ccp.getRemark())?ccp.getRemark():"");
                    data.add(infoList);
                }
                //合计行
                JSONArray totalList = new JSONArray();
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add(clientDeductionPriceTotal);
                totalList.add(supplierDeductionPriceTotal);
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add("");
                totalList.add(priceTotal);
                totalList.add("");
                data.add(totalList);
            }
            obj.put("data", data);
            return R.build(obj);
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取excel的title信息
     * @return excel的title信息
     */
    private JSONArray getTitleStatistics(){
        JSONArray titleList = new JSONArray();
        titleList.add("客诉单号");
        titleList.add("创建日期");
        titleList.add("责任方");
        titleList.add("制单公司");//3
        titleList.add("客户名称");
        titleList.add("操作员");//5
        titleList.add("业务员");//6
        titleList.add("销售合同号");
        titleList.add("产品编码");
        titleList.add("客户扣款金额");
        titleList.add("供应商扣款金额");
        titleList.add("规格编码");
        titleList.add("规格说明");
        titleList.add("中文名称");
        titleList.add("英文名称");
        titleList.add("中文描述");
        titleList.add("英文描述");
        titleList.add("颜色");//17
        titleList.add("单价");
        titleList.add("备注");

        return titleList;
    }
}
